// SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
// SPDX-License-Identifier: Apache-2.0

package roadmap_dao

import (
	"context"

	"gitcode.com/lvupclub/lvup-api/dao/common"
	"gitcode.com/lvupclub/proto-gen-go.git/src/roadmap_plan_api"
	"go.mongodb.org/mongo-driver/v2/bson"
	"go.mongodb.org/mongo-driver/v2/mongo"
	"go.mongodb.org/mongo-driver/v2/mongo/options"
)

type BasicPlanInfoDbItem struct {
	PlanName string `bson:"planName"`
	PlanDesc string `bson:"planDesc"`
}

type PlanInfoDbItem struct {
	PlanId       string              `bson:"_id"`
	RoadmapId    string              `bson:"roadmapId"`
	MemberUserId string              `bson:"memberUserId"`
	BasicInfo    BasicPlanInfoDbItem `bson:"basicInfo"`
	StartTime    int64               `bson:"startTime"`
	EndTime      int64               `bson:"endTime"`
	CreateTime   int64               `bson:"createTime"`
	UpdateTime   int64               `bson:"updateTime"`
}

func (item *PlanInfoDbItem) ToPlanInfo() *roadmap_plan_api.PlanInfo {
	return &roadmap_plan_api.PlanInfo{
		PlanId:       item.PlanId,
		MemberUserId: item.MemberUserId,
		BasicInfo: &roadmap_plan_api.BasicPlanInfo{
			PlanName: item.BasicInfo.PlanName,
			PlanDesc: item.BasicInfo.PlanDesc,
		},
		StartTime:  item.StartTime,
		EndTime:    item.EndTime,
		CreateTime: item.CreateTime,
		UpdateTime: item.UpdateTime,
	}
}

type _PlanInfoDao struct {
	mc *mongo.Collection
}

func newPlanInfoDao(db *mongo.Database) (*_PlanInfoDao, error) {
	mc := db.Collection(common.DB_NS_ROADMAP + "plan")
	_, err := mc.Indexes().CreateOne(context.Background(), mongo.IndexModel{
		Keys: bson.D{{"roadmapId", 1}, {"memberUserId", 1}},
	})
	if err != nil {
		return nil, err
	}
	return &_PlanInfoDao{mc: mc}, nil
}

func (db *_PlanInfoDao) Insert(ctx context.Context, dbItem *PlanInfoDbItem) error {
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_PlanInfoDao) Exist(ctx context.Context, roadmapId, memberUserId, planId string) (bool, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"roadmapId": roadmapId, "memberUserId": memberUserId, "_id": planId})
	return count > 0, err
}

func (db *_PlanInfoDao) Get(ctx context.Context, roadmapId, memberUserId, planId string) (*PlanInfoDbItem, error) {
	retItem := &PlanInfoDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"roadmapId": roadmapId, "memberUserId": memberUserId, "_id": planId}).Decode(retItem)
	return retItem, err
}

func (db *_PlanInfoDao) Remove(ctx context.Context, roadmapId, memberUserId, planId string) error {
	_, err := db.mc.DeleteOne(ctx, bson.M{"roadmapId": roadmapId, "memberUserId": memberUserId, "_id": planId})
	return err
}

func (db *_PlanInfoDao) RemoveByRoadmap(ctx context.Context, roadmapId string) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"roadmapId": roadmapId})
	return err
}

func (db *_PlanInfoDao) Count(ctx context.Context, roadmapId, memberUserId string) (uint32, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"roadmapId": roadmapId, "memberUserId": memberUserId})
	return uint32(count), err
}

func (db *_PlanInfoDao) List(ctx context.Context, roadmapId, memberUserId string) ([]*PlanInfoDbItem, error) {
	cursor, err := db.mc.Find(ctx, bson.M{"roadmapId": roadmapId, "memberUserId": memberUserId}, options.Find().SetSort(bson.M{"startTime": -1}))
	if err != nil {
		return nil, err
	}
	retList := []*PlanInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_PlanInfoDao) UpdateBasic(ctx context.Context, roadmapId, memberUserId, planId string, basicItem *BasicPlanInfoDbItem, updateTime int64) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"roadmapId": roadmapId, "memberUserId": memberUserId, "_id": planId},
		bson.M{"$set": bson.M{
			"basicInfo":  basicItem,
			"updateTime": updateTime,
		}})
	return err
}
